package pe.edu.upc.xcode.util;

import javax.mail.*;
import javax.mail.internet.*;

import org.springframework.stereotype.Repository;

import java.io.FileInputStream;
import java.util.Properties;

public class MailUtil {

	private String hostname;
	private int hostport;
	private String authuser;
	private String authpwd;
	private String protocol;
	private String typetext;
	private boolean debug;
	private String charset;

	public void send(String To, String subject, String msg) throws Exception {

		Properties props = new Properties();
		
		//props.load(new FileInputStream("mail.properties"));
		 
		props.put("mail.transport.protocol", protocol);
		props.put("mail.smtps.host", hostname);
		props.put("mail.smtps.auth", "true");
		// props.put("mail.smtps.quitwait", "false");

		Session mailSession = Session.getDefaultInstance(props);
		mailSession.setDebug(debug);
		Transport transport = mailSession.getTransport();

		MimeMessage message = new MimeMessage(mailSession);
		message.setSubject(subject);
		message.setText(msg, charset, typetext);

		message.addRecipient(Message.RecipientType.TO, new InternetAddress(To));

		transport.connect(hostname, hostport, authuser, authpwd);

		transport.sendMessage(message,
				message.getRecipients(Message.RecipientType.TO));
		transport.close();
	}

	public String getHostname() {
		return hostname;
	}

	public void setHostname(String hostname) {
		this.hostname = hostname;
	}

	public int getHostport() {
		return hostport;
	}

	public void setHostport(int hostport) {
		this.hostport = hostport;
	}

	public String getAuthuser() {
		return authuser;
	}

	public void setAuthuser(String authuser) {
		this.authuser = authuser;
	}

	public String getAuthpwd() {
		return authpwd;
	}

	public void setAuthpwd(String authpwd) {
		this.authpwd = authpwd;
	}

	public String getProtocol() {
		return protocol;
	}

	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}

	public String getTypetext() {
		return typetext;
	}

	public void setTypetext(String typetext) {
		this.typetext = typetext;
	}

	public boolean isDebug() {
		return debug;
	}

	public void setDebug(boolean debug) {
		this.debug = debug;
	}

	public String getCharset() {
		return charset;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}

}